package com.boot.security.server.controller.mffl.MfflLawyer;

import com.boot.security.server.dao.mffl.MfflLawyerCommentDao;
import com.boot.security.server.dao.mffl.MfflLwyerPersonalServiceDao;
import com.boot.security.server.dao.mffl.MfflTypeDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.*;
import com.boot.security.server.model.mffl.Vo.*;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.impl.mffl.Lawyerservicepeoplenumber.LawyerservicepeoplenumberService;
import com.boot.security.server.utils.DataHandleUtil;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "律师功能接口(用户端)")
@RestController
@RequestMapping("/client/mfflTypes")
public class Client_MfflTypeController {

    @Autowired
    private MfflTypeDao mfflTypeDao;

    @Autowired
    private MfflUserDao mfflUserDao;

    @Autowired
    private MfflLawyerCommentDao mfflLawyerCommentDao;

    @Autowired
    private MfflLwyerPersonalServiceDao mfflLwyerPersonalServiceDao;

    @Autowired
    LawyerservicepeoplenumberService LawyerservicepeoplenumberService;

    @GetMapping("/getLawyerType")
    @ApiOperation(value = "获取律师分类")
    public ReturnBean<MfflType> getLawyerType() {
        List<MfflType> mfflTypeList = this.mfflTypeDao.getLawyerType();
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(mfflTypeList);
        return returnBean;
    }


    @GetMapping("/getLawyerList")
    @ApiOperation(value = "律师列表-筛选和搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int"),
            @ApiImplicitParam(required = false, name = "comid", value = "所在地区", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "type", value = "擅长领域", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "order", value = "智能排序：传1 按价格升序 传2 按价格降序", paramType = "query"),
            @ApiImplicitParam(required = false, name = "keyword", value = "搜索关键字(搜索功能只传这个参数和分页,现有关键词名字，城市，律所)", paramType = "query"),
            @ApiImplicitParam(required = false, name = "licensenumber", value = "律所职业证书号", paramType = "query")})

    public ReturnBean<LawyerUser> getLawyerList(PageTableRequest request) {
        if (StringUtils.isEmpty(request.getParams().get("order"))) {
            request.getParams().remove("order");
            request.getParams().put("order", "server_number");
        }

        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset错误");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit错误");
        }
        PageTableResponse tableResponse = new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflUserDao.getLawyerCount(request.getParams());
            }
        }, new ListHandler() {
            @Override
            public List<LawyerUser> list(PageTableRequest request) {
                return mfflUserDao.getLawyerList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        List<LawyerUser> lawyerUserList = (List<LawyerUser>) tableResponse.getData();
        tableResponse.setData(getFieldList(lawyerUserList));
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(tableResponse);
        return returnBean;
    }


    @GetMapping("/getSelectLawyerList")
    @ApiOperation(value = "律师列表-选择律师")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int"),
            @ApiImplicitParam(required = false, name = "comid", value = "所在地区", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "type", value = "擅长领域", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "order", value = "不传 智能排序 传1 按价格升序 传2 按价格降序", paramType = "query"),
            @ApiImplicitParam(required = true, name = "serviceId", value = "服务id 选择律师功能需要 3: 律师函 4：合同审核 5：合同定制 6：图文咨询", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "keyword", value = "搜索关键字(搜索功能只传这个参数和分页,现有关键词名字，城市，律所)", paramType = "query")})
    public ReturnBean<LawyerUser> getSelectLawyerList(PageTableRequest request) {
        if (StringUtils.isEmpty(request.getParams().get("order"))) {
            request.getParams().remove("order");
            request.getParams().put("order", "ORDER BY mu.server_number DESC");
        } else if (request.getParams().get("order").equals("1")) {
            request.getParams().remove("order");
            request.getParams().put("order", "ORDER BY mlps.money DESC");
        } else if (request.getParams().get("order").equals("2")) {
            request.getParams().remove("order");
            request.getParams().put("order", "ORDER BY mlps.money");
        }
        if (StringUtils.isEmpty(request.getParams().get("serviceId"))) {
            return new ReturnBean("201", "serviceId不可为空");
        }
        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset错误");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit错误");
        }
        PageTableResponse tableResponse = new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflUserDao.getLawyerCount(request.getParams());
            }
        }, new ListHandler() {
            @Override
            public List<LawyerUser> list(PageTableRequest request) {
                return mfflUserDao.getSelectLawyerList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        List<LawyerUser> lawyerUserList = (List<LawyerUser>) tableResponse.getData();
        for(int i = 0;i<lawyerUserList.size();i++){
            if(StringUtils.isEmpty(lawyerUserList.get(i).getPrice())){
                lawyerUserList.remove(i);
            }
        }
        tableResponse.setData(getFieldList(lawyerUserList));
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(tableResponse);
        return returnBean;
    }

  /*  @PostMapping("/saveLawyerComment")
    @ApiOperation(value = "律师-添加律师评价(就是订单里对订单的评价)")
    public ReturnBean saveLawyerComment(@ApiParam(value = "律师id", name = "lawyerId", required = true) @RequestParam Integer lawyerId,
                                        @ApiParam(value = "评论级别 好评=1，中评=2，差评=3", name = "rank", required = true) @RequestParam Integer rank,
                                        @ApiParam(value = "订单类型(这个订单是那个律师分类)", name = "type", required = true) @RequestParam Integer type,
                                        @ApiParam(value = "评价内容", name = "content", required = false) @RequestParam String content) {

        if (lawyerId == null) {
            return new ReturnBean("201", "律师id不可为空");
        }
        if (rank == null) {
            return new ReturnBean("201", "律师id不可为空");
        }
        if (type == null) {
            return new ReturnBean("201", "律师id不可为空");
        }


        SysUser sysUser = UserUtil.getLoginUser();
        MfflLawyerComment mfflLawyerComment = new MfflLawyerComment();
        mfflLawyerComment.setRank(rank);
        mfflLawyerComment.setContent(content);
        mfflLawyerComment.setUserId(sysUser.getId().intValue());
        mfflLawyerComment.setLawyerId(lawyerId);
        mfflLawyerComment.setType(type);
        int status = this.mfflLawyerCommentDao.save(mfflLawyerComment);
        if (status == 0) {
            return new ReturnBean("201", "评论失败");
        }

        MfflUser mfflUser = this.mfflUserDao.getUserInfoAll(lawyerId);
        if (mfflUser == null) {
            return new ReturnBean("201", "用户不存在");
        }

        List<MfflLawyerComment> mfflLawyerCommentList = this.mfflLawyerCommentDao.getLawyerCommentListByLawyerId(lawyerId);
        int count = this.mfflLawyerCommentDao.getRankCount(lawyerId);
        String rating = DataHandleUtil.division(count, mfflLawyerCommentList.size());
        this.mfflUserDao.updateRating(rating, lawyerId);
        return new ReturnBean();
    }*/

    @GetMapping(value = "/getLawyerDetails")
    @ApiOperation(value = "律师-律师详情")
    public ReturnBean<LawyerViewVo> getLawyerDetails(@ApiParam(value = "律师id", name = "lawyerId", required = true) @RequestParam Integer lawyerId) {
        //律师信息
        LawyerUserVo lawyerUserVo = this.mfflUserDao.getLawyerUserById(lawyerId);
        if (lawyerUserVo == null) {
            return new ReturnBean("201", "没有该律师");
        }
        //评价信息
        List<MfflLawyerCommentVo> mfflLawyerCommentVoList = this.mfflLawyerCommentDao.getLawyerCommentListByLawyerIdLimit2(lawyerId);
        //服务
        List<MfflLwyerPersonalServiceVo> mfflLwyerPersonalServiceVoList = this.mfflLwyerPersonalServiceDao.getLawyerServiceListByLaeyerId(lawyerId);
        LawyerViewVo lawyerDetailsVo = new LawyerViewVo();
        lawyerDetailsVo.setLawyer(getFieldListVo(lawyerUserVo));
        lawyerDetailsVo.setComment(mfflLawyerCommentVoList);
        lawyerDetailsVo.setService(mfflLwyerPersonalServiceVoList);
        return new ReturnBean("200", lawyerDetailsVo, "成功");
    }

    @GetMapping("/getLawyerComment")
    @ApiOperation(value = "律师-律师详情-律师评论更多")
    public ReturnBean<MfflLawyerCommentVo> getLawyerComment(@ApiParam(value = "律师id", name = "lawyerId", required = true) @RequestParam Integer lawyerId) {
        //评价信息
        List<MfflLawyerCommentVo> mfflLawyerCommentVoList = this.mfflLawyerCommentDao.getLawyerCommentVoListByLawyerId(lawyerId);
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(mfflLawyerCommentVoList);
        return returnBean;
    }


    //获取律师分类 汉字
    private List<LawyerUser> getFieldList(List<LawyerUser> lawyerUserList) {
        for (LawyerUser l : lawyerUserList) {
            if (!StringUtils.isEmpty(l.getField())) {
                String[] typeGroup = l.getField().split(",");

                String fields = "";
                for (String field : typeGroup) {
                    if (field != null && !"".equals(field)) {
                        fields += field + ",";
                    }
                }
                l.setFieldList(this.mfflTypeDao.getLawyerTypeListById(fields.substring(0, fields.length() - 1)));
            }
        }
        return lawyerUserList;
    }

    private LawyerUserVo getFieldListVo(LawyerUserVo lawyerUserVo) {
        if (!StringUtils.isEmpty(lawyerUserVo.getField())) {
        String[] typeGroup = lawyerUserVo.getField().split(",");
            String fields = "";
            for (String field : typeGroup) {
                if (field != null && !"".equals(field)) {
                    fields += field + ",";
                }
            }
            lawyerUserVo.setFieldList(this.mfflTypeDao.getLawyerTypeListById(fields.substring(0, fields.length() - 1)));
        }
        //添加服务人数
        lawyerUserVo.setServerNumber(LawyerservicepeoplenumberService.showLawyerservicepeoplenumber(lawyerUserVo.getId().intValue()).getNumber());
        return lawyerUserVo;
    }


}
